package com.example.qxfw.nyqx.controller;

import com.example.qxfw.common.SysResult;
import com.example.qxfw.common.entity.zy.AgriProduct;
import com.example.qxfw.mongodb.hy.entity.HyDocFile;
import com.example.qxfw.nyqx.service.AgriProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

@Api(tags = "农业气象-产品管理")
@RestController
@CrossOrigin
@RequestMapping("/cpgl")
public class ProductManagementController {

    @Autowired
    private AgriProductService agriProductService;


    @ApiOperation("获取产品")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "bigType",value = "大类：农用天气预报/农事预报",dataType = "String",required = true),
            @ApiImplicitParam(name = "smallType",value = "小类：农用天气预报（产量预报、一周天气文档、月农业天气预报、农业气象灾害预警、病虫害预报、发育期预报）/农事预报（喷药、灌溉、施肥、收晒）",dataType = "String",required = true),
            @ApiImplicitParam(name = "time1",value = "开始时间 yyyy-MM-dd HH:mm:ss",dataType = "String",required = true),
            @ApiImplicitParam(name = "time2",value = "结束时间 yyyy-MM-dd HH:mm:ss",dataType = "String",required = true),
            @ApiImplicitParam(name = "name",value = "文件名称，模糊查询",dataType = "String",required = false)
    })
    @GetMapping("/getProduct")
    public SysResult getProduct(String bigType,String smallType,String time1,String time2,String name){
        List<AgriProduct> agriProducts = new ArrayList<>();
        if(bigType.equals("农用天气预报")){
            agriProducts = agriProductService.listByParam(smallType, time1, time2, name);
        }
        else{
            agriProducts = agriProductService.listByParam3("日农事活动预报", time1, time2, smallType, name);
        }
        if(agriProducts.size()>0){
            return SysResult.success(agriProducts);
        }
        return SysResult.fail("未查询到数据");
    }

    @ApiOperation("删除产品")
    @GetMapping("/deleteProduct")
    public SysResult deleteProduct(String id){
        Integer rows = agriProductService.deleteById(id);
        return SysResult.success(rows);
    }

    /**
     * 得到产品
     * @param id
     * @param response
     * @throws IOException
     * @throws ParseException
     */
    @ApiOperation("根据id得到文档")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "id",value = "文件id",dataType = "String",required = true)
    })
    @PostMapping("/getDocById")
    public void getDocById(String id, HttpServletResponse response) throws IOException, ParseException {
        HyDocFile pdfFile = agriProductService.getDocById(id);
        if (pdfFile != null) {
            InputStream inputStream = new ByteArrayInputStream(pdfFile.getData());
            response.setContentType("application/octet-stream");
            response.addHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode("argiproduct_" + id, "UTF-8"));
            ServletOutputStream outputStream = response.getOutputStream();
            byte[] b = new byte[1024];
            int len;
            while ((len = inputStream.read(b)) > 0) {
                outputStream.write(b, 0, len);
            }
            inputStream.close();
        } else {
            System.out.println("文件不存在");
        }
    }


    @ApiIgnore
    @ApiOperation("插入产品")
    @PostMapping("/saveAgriProduct")
    public SysResult saveAgriProduct(AgriProduct agriProduct){
        return SysResult.success(agriProduct);
    }
}
